package com.test.suanfa;

/**
 * 硬币排列问题，第一行放一个硬币，第二行放两个，以此类推，输入一N个硬币，返回能放几行
 */
public class 硬币排列 {

    public static void main(String[] args) {
        System.out.println(new 硬币排列().fn1(10));
        System.out.println(new 硬币排列().fn2(10));
        System.out.println(new 硬币排列().fn3(10));
    }

    public int fn3(int n) {
        for (int i = 1; i <= n; i++) {
            n = n - i;
            if (n <= i) {
                return i;
            }
        }
        return -1;
    }

    public int fn2(int n) {
        for (int i = 1; i <= n; i++) {
            n = n - i;
            if (n <= i) return i;
        }
        return -1;
    }

    public int fn1(int n) {
        for (int i = 1; i <= n; i++) {
            n = n - i;
            if (n <= i) {
                return i;
            }
        }
        return -1;
    }
}
